package com.dingding.service.banner.core.dao.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.base.dao.impl.BaseDaoImpl;
import com.dingding.common.core.beans.BannerImage;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.service.banner.core.dao.IBannerDao;

/**
 * @ClassName: BannerDaoImpl
 * @Description: Banner操作数据的DAO实现类
 * @author yuHaiQing
 * @date 2016年3月28日 下午1:47:52
 */
@Component("bannerDao")
public class BannerDaoImpl extends BaseDaoImpl<BannerImage> implements IBannerDao {

	/**
	 * @Title: getBannerList
	 * @Description: 获取Banner信息列表
	 * @param bannerShowType 显示类型(1:商城 2:app主页)
	 * @param bannerImageType 图片类型(1:小图  2:大图)
	 * @author yuHaiQing
	 * @return List<Banner>    获取全部的Banner信息
	 */
	@Override
	@Transactional(readOnly = true)
	public List<BannerImage> getBannerList(Integer bannerShowType, Integer bannerImageType) {

		// 设置获取全部活动的条件（有效的活动）
		Criterion criterion = Restrictions.and(Restrictions.eq("bannerShowType", bannerShowType),
				Restrictions.eq("bannerImageType", bannerImageType), Restrictions.eq("bannerStatus", 1));

		// 声明用户存储全部的活动累吧
		List<BannerImage> bannerImages = null;
		try {
			bannerImages = super.queryCriteriaListByOrder(criterion, Order.asc("showSort"));
		} catch (Exception e) {
			log.error("获取全部有效的Banner列表时出现异常，类：" + this.getClass() + " getBannerList()", e);
		}
		
		//拼接图片地址
		if(bannerImages != null && bannerImages.size() > 0){
			for(BannerImage image : bannerImages){
				image.setBannerImageName(ToolUtil.imageAddress(image.getBannerImageName()));
			}
		}

		return bannerImages;
	}

	/**
	 * 
	 * @Title: getBannerListByType 
	 * @Description: 根据模块类型获取Banner列表
	 * @param bannerShowType 显示类型(1:商城 2:app主页)
	 * @param bannerImageType 图片类型(1:小图  2:大图)
	 * @param userType 2:顺风车 3:出租车
	 * @author YHQ 
	 * @since V2.5.5
	 * @return List<BannerImage>    返回类型
	 * 
	 */
	@Override
	@Transactional(readOnly = true)
	public List<BannerImage> getBannerListByType(Integer bannerShowType, Integer bannerImageType,Integer userType) {

		// 设置获取全部活动的条件（有效的活动）
		Criterion criterion = Restrictions.and(Restrictions.eq("bannerShowType", bannerShowType),
				Restrictions.eq("bannerImageType", bannerImageType), Restrictions.eq("bannerStatus", 1),Restrictions.eq("useType", userType));

		// 声明用户存储全部的活动累吧
		List<BannerImage> bannerImages = null;
		try {
			bannerImages = super.queryCriteriaListByOrder(criterion, Order.asc("showSort"));
		} catch (Exception e) {
			log.error("获取全部有效的Banner列表时出现异常，类：" + this.getClass() + " getBannerList()", e);
		}

		// 创建响应Bean对象
		List<BannerImage> bannerList = new ArrayList<BannerImage>();
		
		// 遍历查询结果，修改BannerName
		if(bannerImages != null && bannerImages.size() > 0){
			
			for (int i=0; i<bannerImages.size(); i++) {
				
				BannerImage bannerImage = bannerImages.get(i);
				
				// 修改图片访问地址
				bannerImage.setBannerImageName(ToolUtil.imageAddress(bannerImage.getBannerImageName()));
				
				bannerList.add(bannerImage);
			}
			
		}
		
		return bannerList;
	}
	
	
	/**
	 * @Title: getBannerInfo
	 * @Description: 获取Banner详情
	 * @param bannerImageId Banner主键Id
	 * @author yuHaiQing
	 * @return Banner    Banner详情信息
	 */
	@Override
	@Transactional(readOnly = true)
	public BannerImage getBannerInfo(String bannerImageId) {

		// 声明存储活动详情的对象
		BannerImage bannerImage = null;

		try {
			// 根据活动Id获取活动详情
			bannerImage = super.getById(bannerImageId);

		} catch (Exception e) {
			log.error("根据Banner Id获取Banner详情时出现异常  类：" + this.getClass() + " getBannerInfo()", e);
		}

		return bannerImage;
	}

}
